import com.sun.org.apache.xpath.internal.operations.String;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

public class UpdateNodeData {


    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    private static ZooKeeper zooKeeper;

    public void process(WatchedEvent watchedEvent) {

        if(watchedEvent.getType() == Watcher.Event.EventType.NodeChildrenChanged){

        }


        if(watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected){
            try {
                updateNodeData();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void updateNodeData(){
        try {
            byte[] data = zooKeeper.getData("/test", false, null);
            System.out.println(new java.lang.String(data));
            //stat 状态信息对象
            Stat stat = zooKeeper.setData("/test", "sdfsad".getBytes(), -1);

        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    public void  deleteNodeData() throws Exception {
        Stat stat = zooKeeper.exists("/test", false);
        if(Objects.nonNull(stat)){
            zooKeeper.delete("/test",-1);

        }

    }

    public static void main(String[] args) throws Exception{
        zooKeeper = new ZooKeeper("localhost:2181", 5000, new ZookeeperSession());
        Thread.sleep(Integer.MAX_VALUE);
    }

}
